package com.sm.healthy.utils;

import android.text.TextUtils;

import java.util.UUID;

public class StringUtils {


    /**
     * 生成UUID
     */
    public static String getGenerateUUID(){
        return UUID.randomUUID().toString();
    }

    /**
     * 数组转16进制字符串
     *
     * @param b param length
     * @return
     */
    public static String byte2HexStr(byte[] b) {
        StringBuilder hs = new StringBuilder();
        String stmp = "";
        for (byte value : b) {
            stmp = Integer.toHexString(value & 0xFF);
            if (stmp.length() == 1)
                hs.append("0").append(stmp);
            else {
                hs.append(stmp);
            }
        }
        return hs.toString().toUpperCase();
    }

    /**
     * 16进制转10进制
     *
     * @param msg
     * @return
     */
    public static Long getValue16To10(String msg) {
        return Long.parseLong(msg, 16);
    }

    /**
     * 字符串转16进制
     *
     * @param str
     * @return
     */
    public static byte[] stringTo16Byte2(String str) {
        String hex = str;
        int len = hex.length() / 2;
        if (hex.length() % 2 == 1) {
            len = len + 1;
            hex = "0" + hex;
        }
        char[] chars = hex.toCharArray();
        String[] hexs = new String[len];
        byte[] bytes = new byte[len];
        for (int i = 0, j = 0; j < len; i = i + 2, j++) {
            hexs[j] = "" + chars[i] + chars[i + 1];
            bytes[j] = (byte) (Integer.parseInt(hexs[j], 16)); // Convert.ToInt16(hexes[j],
            // 16)
        }
        return bytes;
    }

    /**
     * 把byte转化成2进制字符串
     *
     * @param b
     * @return
     */
    public static String getBinaryStrFromByte(byte b) {
        String result = "";
        byte a = b;
        ;
        for (int i = 0; i < 8; i++) {
            byte c = a;
            a = (byte) (a >> 1);//每移一位如同将10进制数除以2并去掉余数。
            a = (byte) (a << 1);
            if (a == c) {
                result = "0" + result;
            } else {
                result = "1" + result;
            }
            a = (byte) (a >> 1);
        }
        return result;
    }

    /**
     * 字符串转换成十六进制字符串
     * param String str 待转换的ASCII字符串
     *
     * @return String 每个Byte之间空格分隔，如: [61 6C 6B]
     */
    public static String str2HexStr(String str) {

        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;

        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            sb.append(' ');
        }
        return sb.toString().trim();
    }


    /**
     * ASCII 转16进制
     * @param str
     * @return
     */
    public String convertStringToHex(String str){

        char[] chars = str.toCharArray();

        StringBuffer hex = new StringBuffer();
        for(int i = 0; i < chars.length; i++){
            hex.append(Integer.toHexString((int)chars[i]));
        }

        return hex.toString();
    }


    /**
     * 16进制转ASCII码
     * @param hex
     * @return
     */
    private static String convertHexToString(String hex){

        StringBuilder sb = new StringBuilder();
        StringBuilder temp = new StringBuilder();

        //49204c6f7665204a617661 split into two characters 49, 20, 4c...
        for( int i=0; i<hex.length()-1; i+=2 ){

            //grab the hex in pairs
            String output = hex.substring(i, (i + 2));
            //convert hex to decimal
            int decimal = Integer.parseInt(output, 16);
            //convert the decimal to character
            sb.append((char)decimal);

            temp.append(decimal);
        }

        return sb.toString();
    }


    /////////////////////////////////////////  截取响应命令字符串 返回界面 /////////////////////////////////////////////////

    /**
     * 截取命令字
     */
    private static String subFlagStr(String flag) {

        if (!TextUtils.isEmpty(flag)) {
            flag = flag.substring(4, 6);
        }

        return flag;
    }

}
